package edu.base.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.base.util.HigherResponse;
import edu.base.dao.RecEqptDao;
import edu.base.entity.RecordEquipment;

@Service
public class RecEqptServiceImpl implements RecEqptService{
	
	@Autowired
	private RecEqptDao recEqptDao;
	
	//分页查询所有设备
	@Override
	public HigherResponse<PageInfo<RecordEquipment>> queryEqptByPage(Integer pageNum, Integer pageSize) {
		//step1  开启分页插件
		PageHelper.startPage(pageNum, pageSize);
		//step2  查询设备
		List<RecordEquipment> queryAllEqpts=recEqptDao.queryAllEqpt();
		//step3 将查询到的信息放入pageInfo
		PageInfo<RecordEquipment> pageInfo=new PageInfo<RecordEquipment>(queryAllEqpts);
		if(pageInfo.getSize()==0) {
			return HigherResponse.getFailedRespon("无记录");
		}
		return HigherResponse.getSuccessRespon(pageInfo);
	}
	
	//根据日期和产品类型查询设备
	@Override
	public HigherResponse<PageInfo<RecordEquipment>> queryEqptByDate(String beginDate, String endDate,
			Integer prodType, Integer pageNum, Integer pageSize) {
		//判断客服是否登录
		PageHelper.startPage(pageNum, pageSize);
		List<RecordEquipment> rec=recEqptDao.queryEqptByDate(beginDate, endDate, prodType);
		PageInfo<RecordEquipment> pageInfo=new PageInfo<RecordEquipment>(rec);
		//System.out.println("pageInfo:"+pageInfo.getSize());
		if(pageInfo.getSize()==0) {
			return HigherResponse.getFailedRespon("无记录");
		}
		return HigherResponse.getSuccessRespon(pageInfo);
	}
	
	//根据id查询设备信息
	@Override
	public HigherResponse<RecordEquipment> queryEqptById(Integer id) {
		//判断客服是否登录
		RecordEquipment rec=recEqptDao.queryEqptById(id);
		if(null==rec) {
			return HigherResponse.getFailedRespon("查询失败");
		}
		return HigherResponse.getSuccessRespon(rec);
	}
	
	//根据id修改设备信息
	@Override
	public HigherResponse<String> updateEqptById(RecordEquipment receqpt) {
		boolean flag=recEqptDao.updateEqptById(receqpt);
		if(false==flag) {
			return HigherResponse.getFailedRespon("修改失败");
		}
		return HigherResponse.getSuccessRespon("修改成功");
	}
	
	//删除设备for real
	@Override
	public HigherResponse<String> deleteOneEqptById(Integer id) {
		int flag=recEqptDao.deleteOneEqptById(id);
		if(0==flag) {
			return HigherResponse.getFailedRespon("删除失败");
		}
		return HigherResponse.getSuccessRespon("删除成功");
	}
	
	//删除设备 fake
	@Override
	public HigherResponse<String> deleteEqptById(Integer id) {
		boolean flag=recEqptDao.deleteEqptById(id, 2);
		if(false==flag) {
			return HigherResponse.getFailedRespon("删除失败");
		}
		return HigherResponse.getSuccessRespon("删除成功");
	}
	
	//添加设备
	@Override
	public HigherResponse<String> insertEqpt(RecordEquipment receqpt) {
		int flag=recEqptDao.insertEqpt(receqpt);
		if(0==flag) {
			return HigherResponse.getFailedRespon("添加失败");
		}
		return HigherResponse.getSuccessRespon("添加成功");
	}
	
	//根据报修编号查询设备
	@Override
	public HigherResponse<PageInfo<RecordEquipment>> queryEqptByRepairId(Integer repairId, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<RecordEquipment> rec=recEqptDao.queryEqptByRepairId(repairId);
		PageInfo<RecordEquipment> pageInfo=new PageInfo<RecordEquipment>(rec);
		if(pageInfo.getSize()==0) {
			return HigherResponse.getFailedRespon("无记录");
		}
		return HigherResponse.getSuccessRespon(pageInfo);
	}
	
	//根据报修id产品类型查询设备
	@Override
	public HigherResponse<PageInfo<RecordEquipment>> queryEqptByProdType(Integer repairId, Integer prodType,
			Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<RecordEquipment> rec=recEqptDao.queryEqptByProdType(repairId, prodType);
		PageInfo<RecordEquipment> pageInfo=new PageInfo<RecordEquipment>(rec);
		if(pageInfo.getSize()==0) {
			return HigherResponse.getFailedRespon("无记录");
		}
		return HigherResponse.getSuccessRespon(pageInfo);
	}
	
	
}
